import request from '@/utils/request';

export const getDeviceList = (currentPage: number, count: number) => {
  return request({
    url: `/api/device/query/devices`,
    method: 'get',
    params: {
      page: currentPage,
      count: count
    }
  });
};
export const getDevice = (deviceId: string | number) => {
  return request({
    url: `/api/device/query/devices/${deviceId}`,
    method: 'get'
  });
};

export const getAllDeviceList = async () => {
  const currentPage = 1;
  const count = 100;
  const deviceList: any[] = [];
  return await getAllDeviceListIteration(deviceList, currentPage, count);
};
const getAllDeviceListIteration = async (deviceList: any[], currentPage: number, count: number) => {
  const data = await getDeviceList(currentPage, count);
  if (data.rows) {
    deviceList = deviceList.concat(data.rows);
    if (deviceList.length < data.total) {
      currentPage++;
      await getAllDeviceListIteration(deviceList, currentPage, count);
    } else {
      return deviceList;
    }
  }
};
export const getAllChannel = (isCatalog: any, catalogUnderDevice: any, deviceId: any) => {
  const currentPage = 1;
  const count = 100;
  const catalogList: any[] = [];
  getAllChannelIteration(isCatalog, catalogUnderDevice, deviceId, catalogList, currentPage, count);
};
const getAllChannelIteration = (isCatalog: any, catalogUnderDevice: any, deviceId: any, catalogList: any[], currentPage: number, count: number) => {
  getChanel(isCatalog, catalogUnderDevice, deviceId, currentPage, count).then((data) => {
    if (data.data.list) {
      catalogList = catalogList.concat(data.data.list);
      if (catalogList.length < data.total) {
        currentPage++;
        getAllChannelIteration(isCatalog, catalogUnderDevice, deviceId, catalogList, currentPage, count);
      } else {
        return catalogList;
      }
    }
  });
};
const getChanel = (isCatalog: any, catalogUnderDevice: any, deviceId: any, currentPage: number, count: number) => {
  return request({
    url: `/api/device/query/devices/${deviceId}/channels`,
    method: 'get',
    params: {
      page: currentPage,
      count: count,
      query: '',
      online: '',
      channelType: isCatalog,
      catalogUnderDevice: catalogUnderDevice
    }
  });
};
export const getAllSubChannel = (isCatalog: any, deviceId: any, channelId: any) => {
  const currentPage = 1;
  const count = 100;
  const catalogList: any[] = [];
  getAllSubChannelIteration(isCatalog, deviceId, channelId, catalogList, currentPage, count);
};
const getAllSubChannelIteration = (isCatalog: any, deviceId: any, channelId: any, catalogList: any[], currentPage: number, count: number) => {
  getSubChannel(isCatalog, deviceId, channelId, currentPage, count).then((data) => {
    if (data.data.list) {
      catalogList = catalogList.concat(data.data.list);
      if (catalogList.length < data.total) {
        currentPage++;
        getAllSubChannelIteration(isCatalog, deviceId, channelId, catalogList, currentPage, count);
      } else {
        return catalogList;
      }
    }
  });
};
const getSubChannel = (isCatalog: any, deviceId: any, channelId: any, currentPage: number, count: number) => {
  return request({
    url: `/api/device/query/sub_channels/${deviceId}/${channelId}/channels`,
    method: 'get',
    params: {
      page: currentPage,
      count: count,
      query: '',
      online: '',
      channelType: isCatalog
    }
  });
};
export const getTree = async (deviceId: any, parentId: any, onlyCatalog: any) => {
  const currentPage = 1;
  const count = 100;
  const catalogList: any[] = [];
  return await getTreeIteration(deviceId, parentId, onlyCatalog, catalogList, currentPage, count);
};
const getTreeIteration = async (deviceId: any, parentId: any, onlyCatalog: any, catalogList: any[], currentPage: number, count: number) => {
  const data = await getTreeInfo(deviceId, parentId, onlyCatalog, currentPage, count);
  console.log('data.data.list', data.data.list);
  if (data.data.list) {
    catalogList = catalogList.concat(data.data.list);
    if (catalogList.length < data.total) {
      currentPage++;
      await getTreeIteration(deviceId, parentId, onlyCatalog, catalogList, currentPage, count);
    } else {
      return catalogList;
    }
  }
};

const getTreeInfo = (deviceId: any, parentId: any, onlyCatalog: any, currentPage: number, count: number) => {
  return request({
    url: `/api/device/query/tree/${deviceId}`,
    method: 'get',
    params: {
      page: currentPage,
      count: count,
      query: '',
      online: '',
      channelType: onlyCatalog
    }
  });
};
export const addDevice = (device: any) => {
  return request({
    url: `/api/device/query/device/add/`,
    method: 'post',
    data: device
  });
};
export const updateDevice = (device: any) => {
  return request({
    url: `/api/device/query/device/update/`,
    method: 'put',
    data: device
  });
};
export const syncStatus = (deviceId: any) => {
  return request({
    url: `/api/device/query/${deviceId}/sync_status`,
    method: 'get'
  });
};
export const updateTransport = (deviceId: any, streamMode: any) => {
  return request({
    url: `/api/device/query/transport/${deviceId}/${streamMode}`,
    method: 'post'
  });
};
export const syncDevice = (deviceId: any) => {
  return request({
    url: `/api/device/query/devices/${deviceId}/sync`,
    method: 'get'
  });
};
export const deleteDevice = (deviceId: any) => {
  return request({
    url: `/api/device/query/devices/${deviceId}/delete`,
    method: 'delete'
  });
};
